/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package company.search.beans;

import company.globalEvents.PaidCompanyProductEvent;
import facades.CandidateFacade;
import facades.SearchFacade;
import entities.Candidate;
import entities.Company;
import entities.Search;
import javax.inject.Named;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.enterprise.context.SessionScoped;
import javax.enterprise.event.Event;
import javax.enterprise.event.Observes;
import javax.inject.Inject;
import qualifiers.ChosenEntityEvent;
import qualifiers.LoggedIn;
import qualifiers.PreCreateEntityEvent;
import sqlgenerators.search.SearchSQLGenerator;
import sqlgenerators.search.SearchSqlGeneratorInterface;

/**
 *
 * @author damfercer
 */
@Named
@SessionScoped
public class SearchListBean implements Serializable {
    @Inject
    private CandidateFacade candidateEJB;
    @Inject
    private SearchFacade searchEJB;
    private SearchSqlGeneratorInterface sqlGenerator;
    private Search selectedSearch;
    private List<Candidate> previewCandidateList;
    @Inject
    @LoggedIn
    private Company company;
    @Inject
    @ChosenEntityEvent
    private Event<Search> selectSearchEvent;
    @Inject
    @PreCreateEntityEvent
    private Event<Integer> preNewSearchEvent;
    /**
     * Creates a new instance of SearchListBean
     */
    public SearchListBean() {
        sqlGenerator=new SearchSQLGenerator();
        previewCandidateList=new ArrayList<Candidate>();
    }
    
    public Company getCompany() {
        return company;
    }
    
    public String goToNewSearch(){
        preNewSearchEvent.fire(0);
        return "/views/company/search/CreateSearchView?faces-redirect=true";
    }
    
    private List<Candidate> getPreviewCandidates(){
        String sql=sqlGenerator.getSqlFromSearch(selectedSearch);
        List<Candidate> searchCandidates = candidateEJB.getPreviewSearchCandidates(sql, 6, getCompany());
        return searchCandidates;  
    }
    
    public void setSearchAndSearchCandidates(Search selected){
        setSelectedSearch(selected);
        setPreviewCandidateList(getPreviewCandidates());
    }
    
    public void deleteSearch(){
        getCompany().getSearchList().remove(getSelectedSearch());
        searchEJB.remove(getSelectedSearch());
        selectedSearch=null;
    }
    
    public void toggleActiveSearch(Boolean active){
        selectedSearch.setActive(active);
        searchEJB.edit(selectedSearch);
    }
    
    public String viewSearch(){
        selectSearchEvent.fire(selectedSearch);
        return "/views/company/search/ViewSearchView?faces-redirect=true";
    }
    
    public void editSearchName(Search search){
        searchEJB.edit(search);
    }

    /**
     * @return the selectedSearch
     */
    public Search getSelectedSearch() {
        return selectedSearch;
    }

    /**
     * @param selectedSearch the selectedSearch to set
     */
    public void setSelectedSearch(Search selectedSearch) {
        this.selectedSearch = selectedSearch;
    }

    /**
     * @return the previewCandidateList
     */
    public List<Candidate> getPreviewCandidateList() {
        return previewCandidateList;
    }

    /**
     * @param previewCandidateList the previewCandidateList to set
     */
    public void setPreviewCandidateList(List<Candidate> previewCandidateList) {
        this.previewCandidateList = previewCandidateList;
    }
    
    public Integer getNumberOfColumns(){
        return Math.min(4, this.getCompany().getSearchList().size());
    }

}
